package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.MovementsApi;
import com.tanhua.dubbo.utils.IdService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MovementsApiImpl implements MovementsApi {
    @Autowired
    private IdService idService;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void saveMovement(Movement movement) {

        Long userId = movement.getUserId(); //当前登录人id
//        1、保存动态详情
        movement.setId(new ObjectId());
        Long pid = idService.getNextId("movement");
        movement.setPid(pid);
        movement.setCreated(new Date().getTime());
        mongoTemplate.save(movement);
//        2、保存自己的动态表
        MovementMine movementMine = new MovementMine();
        movementMine.setId(new ObjectId());
        movementMine.setMovementId(movement.getId()); //外键
        movementMine.setCreated(new Date().getTime());
        mongoTemplate.save(movementMine,"movement_mine_"+userId);
//        3、查询好友
//        db.firend.find({"userId":userid}).sort({created:-1})
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
//        4、保存好友的动态表
//        假如1号用户的好友是2号 3号  4号  保存的表名是：movement_friend_2  movement_friend_3  movement_friend_4
        for (Friend friend : friendList) {
            MovementFriend movementFriend = new MovementFriend();
            movementFriend.setId(new ObjectId());
            movementFriend.setMovementId(movement.getId()); //外键
            movementFriend.setCreated(new Date().getTime());
            movementFriend.setUserId(userId); //哪个好友发布的动态
            mongoTemplate.save(movementFriend,"movement_friend_"+friend.getFriendId());
        }
//
    }

    @Override
    public PageResult queryFriendMovements(Long userId, Integer page, Integer pagesize) {
//        1、查询movement_friend_USERID
//                movementId
//         2、根据movementId查询movement_detail

        Query query = new Query();
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        List<MovementFriend> list = mongoTemplate.find(query, MovementFriend.class, "movement_friend_" + userId);
        long count = mongoTemplate.count(query, MovementFriend.class, "movement_friend_" + userId);

        List<Movement> movementList = new ArrayList<>();
        for (MovementFriend movementFriend : list) {
            ObjectId movementId = movementFriend.getMovementId();
            Movement movement = mongoTemplate.findById(movementId, Movement.class);
            if(movement!=null){
                movementList.add(movement);
            }

        }
        return new PageResult(page,pagesize,(int)count,movementList);
    }

    @Override
    public PageResult queryRecommendMovements(Long userId, Integer page, Integer pagesize) {
        //        1、查询 recommend_movement
//               db.recommend_movement.find({"userId":USERID}).sort({"created":-1}).limit(pagesize).skip( (page-1)*pagesize )
//         2、根据movementId查询movement_detail

        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        List<RecommendMovement> list = mongoTemplate.find(query, RecommendMovement.class);
        long count = mongoTemplate.count(query, RecommendMovement.class);

        List<Movement> movementList = new ArrayList<>();
        for (RecommendMovement recommendMovement : list) {
            ObjectId movementId = recommendMovement.getMovementId();
            Movement movement = mongoTemplate.findById(movementId, Movement.class);
            if(movement!=null){
                movementList.add(movement);
            }

        }
        return new PageResult(page,pagesize,(int)count,movementList);
    }

    @Override  //查询自己的动态
    public PageResult queryMyselfMovements(Long userId, Integer page, Integer pagesize) {
//        1、查询movement_mine_USERID
//                movementId
//         2、根据movementId查询movement_detail

        Query query = new Query();
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        List<MovementMine> list = mongoTemplate.find(query, MovementMine.class, "movement_mine_" + userId);
        long count = mongoTemplate.count(query, MovementMine.class, "movement_mine_" + userId);

        List<Movement> movementList = new ArrayList<>();
        for (MovementMine movementMine : list) {
            ObjectId movementId = movementMine.getMovementId();
            Movement movement = mongoTemplate.findById(movementId, Movement.class);
            if(movement!=null){
                movementList.add(movement);
            }

        }
        return new PageResult(page,pagesize,(int)count,movementList);
    }

    @Override
    public Movement queryMovementById(String movementId) {
//        根据id查询
        return mongoTemplate.findById( new ObjectId(movementId) , Movement.class);
    }
}
